import { Injectable, UseFilters } from '@nestjs/common';
import { CreateSetingDto, GetSetingDto, UpdateSetingDto } from './dto/seting.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { IdDto } from 'src/common/dto/dto';
import { HttpExceptionFilter } from 'src/common/http-exception.filter';
import { throwHttp } from 'src/utlis';
import { isTrue } from 'src/utlis/tools';
import { Repository } from 'typeorm';
import { SetingEntity } from './entities/seting.entity';

@Injectable()
export class SetingService {
    constructor(
        @InjectRepository(SetingEntity)
        private setingEntityRepository: Repository<SetingEntity>,
    ) { }
    async getSeting(params: GetSetingDto) {
        try {
            const {
                code,
            } = params;
            const data = await this.setingEntityRepository.findOne({
                where: {
                    code,
                },
                cache: true,
                order: { createDate: 'DESC' },
            });
            return data?.data ?? {}
        } catch (error) {
            throwHttp(error.message);
        }
    }
    /** 创建系统设置 */
    @UseFilters(new HttpExceptionFilter())
    async createSeting(body: CreateSetingDto) {
        try {
            const { code, data, } = body;
            const seting = new SetingEntity();
            seting.code = code;
            seting.data = data;
            const result = await this.setingEntityRepository.manager.save(seting);
            if (isTrue(result)) {
                return '创建成功';
            }
            return '创建失败';
        } catch (error) {
            throwHttp(error.message);
        }
    }
    /** 更新系统设置 */
    @UseFilters(new HttpExceptionFilter())
    async updateSeting(body: UpdateSetingDto) {
        try {
            const { id, code, data } = body;
            const SetingInfo = await this.getSetingInfo({
                id,
            });
            if (!isTrue(SetingInfo)) {
                throwHttp('系统设置不存在');
                return;
            }
            const { affected } = await this.setingEntityRepository
                .createQueryBuilder()
                .update(SetingEntity)
                .set({
                    code, data
                })
                .where('id = :id', { id })
                .execute();
            if (affected) {
                return '更新成功';
            }
            throwHttp('更新失败');
        } catch (error) {
            throwHttp(error.message);
        }
    }
    /** 获取系统设置详情 */
    @UseFilters(new HttpExceptionFilter())
    async getSetingInfo(where) {
        try {
            return await this.setingEntityRepository.findOne({
                where,
                cache: true,
            });
        } catch (error) {
            throwHttp(error.message);
        }
    }
    /** 删除系统设置 */
    @UseFilters(new HttpExceptionFilter())
    async deleteSeting({ id }: IdDto) {
        try {
            const data = await this.getSetingInfo({ id });
            if (!isTrue(data)) {
                throwHttp('系统设置不存在');
                return;
            }
            await this.setingEntityRepository.softDelete(id);
            return '删除成功';
        } catch (error) {
            throwHttp(error.message);
        }
    }
}
